﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

namespace Tools
{
    public class MakeJson
    {
        public static JArray JsonToLinqArray(string json)
        {
            return JArray.Parse(json);
        }

        /// <summary>
        /// Json读取为Linq数据
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static JObject JsonToLinqObject(string json)
        {
            JObject obj = JObject.Parse(json, new JsonLoadSettings()
            {

            });
            return obj;
        }

        /// <summary>
        /// JSON转换成Obj
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(string str)
        {
            return JsonConvert.DeserializeObject<T>(str);
        }

        /// <summary>
        /// OBJ转换到JSON
        /// </summary>
        /// <param name="ob"></param>
        /// <returns></returns>
        public static string ObjectToJson(object ob, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff")
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = dateTimeFormat;
            return JsonConvert.SerializeObject(ob, timeFormat);
        }
    }
}